package com.ias.agdis_r.view.base;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.ias.agdis_r.R;
import com.ias.agdis_r.bean.other.TempLabelBean;
import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.manager.MessageManager;
import com.ias.agdis_r.manager.ServiceManage;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.util.S;

public abstract class BaseGroupView extends LinearLayout {

	protected int viewNum = 0;
	protected String jfk = ""; // 接飞航班唯一号
	public ProgressBar progress; // 进度条
	private Context context;

	public BaseGroupView(Context context, ProgressBar progress) {
		super(context);
		this.context = context;
		this.progress = progress;
		init();
		// TODO Auto-generated constructor stub
	}

	/**
	 * 初始化
	 */
	protected abstract void init();

	/**
	 * 根据TAG增加VIEW
	 */
	public abstract boolean addViewByTag(String infoTag, TempLabelBean labelBean);

	/**
	 * 获取Layout中的View数量
	 */
	public abstract int getViewNum();

	public void setOnClickListener(View view, String action) {
		view.setOnClickListener(new GroupOnClickListener(action));
	}

	class GroupOnClickListener implements OnClickListener {

		private String action;

		public GroupOnClickListener(String action) {
			this.action = action;
		}

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			viewClick(action, progress);
		}
	}

	protected void viewClick(String action, ProgressBar progress) {
		if (MessageModel.IAS_FLIGHT_QUERY.equals(action)) {
			// 如果显示了进度条，不执行动作
			if (progress != null && progress.getVisibility() == View.VISIBLE) {
				return;
			}
			if (S.isNotEmpty(jfk)) {
				EffectManager.getInstance().ttsPlay(
						EffectManager.TTS_TYPE_TASK, context.getString(R.string.searchFlight), false);
				String lastSearchFlightMessage = MessageManager.getInstance()
						.getSearchFlightMessage(MessageModel.RKE, jfk, "0", 1,
								"N", "", "", null, null);
				ServiceManage.getInstance().sendMessage(
						lastSearchFlightMessage, true, progress);
			} else {
				EffectManager.getInstance().ttsPlay(
						EffectManager.TTS_TYPE_TASK, context.getString(R.string.noFlightResult), false);
			}
		}
	};

	public void setJFK(String jfk) {
		this.jfk = jfk;
	};
}
